<?php
 
 class Feed_Model_Index extends Zend_Db_Table
 {
 	protected $_name="feeduser";
    protected $_primary = 'id';
    
 	public function insertFeed($arrParam = null, $option = null)
  	{	
  		$row = $this->fetchNew();
  		$row->userfeedid           =@$arrParam['userfeedid'];			
		$row->postid           =@$arrParam['postid'];
		$row->createdate       =@$arrParam['createdate'];
		$row->status       =@$arrParam['status'];
		$row->time_send_mail       =@$arrParam['time_send_mail'];
		$row->number_sendmail       =@$arrParam['number_sendmail'];
		
	  	return $row->save();
	  	
  	}
  	
 	public function listItem($arrParam = null, $option = null){
		
		$db = Zend_Registry::get('connectDB');
				
  		$sql = "select * from chanel";
  		$result = $db->fetchAll($sql);
		
  		return $result;
  	}
  	
  	public function findItem($val){
  		$db = Zend_Registry::get('connectDB');
  		$sql = "select * from chanel where type LIKE '%".$val."%'";
  		$result = $db->fetchAll($sql);
		
  		return $result;
  	}
  	
 	public function updateFeed($arrParam = null, $option = null)
  	{ 		
  		
  		$db = Zend_Registry::get('connectDB');	
		$where = array(
    		'id = ?' => $arrParam['id'],
			'userfeedid = ?' => $arrParam['userfeedid'],		
		);	
  		$data = array();
			
		if (@$arrParam['postid'] != ""){
			$data["postid"]=$arrParam['postid'];
		}
		
		if (@$arrParam['userfeedid'] != ""){
			$data["userfeedid"]=$arrParam['userfeedid'];
		}
		
		if (@$arrParam['createdate'] != ""){
			$data["createdate"]=$arrParam['createdate'];
		}
		
		if (@$arrParam['status'] != ""){
			$data["status"]=$arrParam['status'];
		}			
		if (@$arrParam['time_send_mail'] != ""){				
			$data["time_send_mail"]=$arrParam['time_send_mail'];				
		}			
		if (@$arrParam['number_sendmail'] != ""){
			$data["number_sendmail"]=$arrParam['number_sendmail'];
		}
  		
		return $db->update("feeduser", $data, $where); 		
  	  	
  	}
  	
  	public function updateStatusFeed($arrParam){
  		$db = Zend_Registry::get('connectDB');	
		$where = array(
    		'status = ?' => "new",
			'id = ?' => $arrParam['id'],		
		);	
  		$data = array();
  		$data["status"]=$arrParam['status'];
  		$db->update("feeduser", $data, $where);
  	}
  	
 	public function getChanelById($id){	
  		$db = Zend_Registry::get('connectDB');		
		$sql = "SELECT *  FROM chanel where id =  '".$id."'";
  		
		return $db->fetchRow($sql);
	  		
	}
	
	public function sendEmail($user,$chanel_item) {

		$email = new Email_Model_Index();
		
		$transport = $email->connectSmtp();
        
        // create view object
		$html = new Zend_View();
		$html->setScriptPath(APPLICATION_PATH.'/modules/feed/views/scripts/mail/');
		
		
		
		//channels
		
		$chanel = new Chanel_Model_Index();		
		
		$page = 1;
		
		$pagesize = 6;
		
		$arr = array(		
			"userid"=>$user["id"],		
			"page"=> $page,
			"pagesize"=> $pagesize,
																
		);
		
		
		$list_chanel = $chanel->listItem($arr);
		
		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();
		
		
		// list post
		$post = new Post_Model_Index();		
		
		$page = 1;
		
		$pagesize = 3;
		
		$arr = array(		
			"userid"=>$user["id"],
			"chanelid"=>$chanel_item["id"],		
			"page"=> $page,
			"pagesize"=> $pagesize,
																
		);
		
		$list_post = array();
		
		$list_userid = $post->getPostByChanel($arr);
		
		foreach($list_userid as $key => $val){
			
			$arr = array(		
			"userid"=>$val["userid"],
			"chanelid"=>$chanel_item["id"],
																
			);
			
			array_push($list_post,$post->getPostByChanelUserTop($arr));
			
		}
		
		$str_name = "";
		
		for($i =0 ; $i < count($list_post); $i++){

			if($i != 0){
				$str_name .= ", ".$list_post[$i]["displayname"];	
			}else{
				$str_name .= $list_post[$i]["displayname"];
			}
		}
		
		
		
				
		// assign valeues
		$html->assign('chanel', $chanel_item);
		$html->assign('user', $data);
		$html->assign('list_chanel', $list_chanel);
		$html->assign('list_post', $list_post);
			
		$mail = new Zend_Mail('utf-8');
		
		// render view
		$bodyText = $html->render('feed.phtml');
		
		$mail->setFrom('channel@otaha.com', 'Otaha');
		$mail->addTo($user["email"]);		
		$mail->setSubject($user["displayname"].",Có bạn ".$str_name." chia sẻ trên kênh ".$chanel_item["name"]);
		$mail->setBodyHtml($bodyText);		
		$mail->send($transport);
		 
    } 
	
  	
 }
 
 ?>